package com.example.printer2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.printer2.adapter.FileListAdapter;
import com.example.printer2.common.MyFileUtil;
import com.example.printer2.common.ResultContract;
import com.example.printer2.po.MyFile;
import com.example.printer2.po.My_List;
import com.example.printer2.po.Record;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class FileActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    FileListAdapter adapter;
    Switch aSwitch;
    Button pay;
    TextView total_price;
    int id=1;
    private List<MyFile> list=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file);
        recyclerView=findViewById(R.id.listview);
        total_price=findViewById(R.id.total_price);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter=new FileListAdapter(list,handler,this);
        recyclerView.setAdapter(adapter);
        aSwitch=findViewById(R.id.switch1);
        pay=findViewById(R.id.pay);
        init();
        Intent intent=getIntent();
        id=intent.getIntExtra("id",1);
    }

    public void init()
    {
        pay.setOnClickListener(onClickListener);
        aSwitch.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId())
            {
                case R.id.switch1:
                {
                    updatePrice();
                    break;
                }
                case R.id.pay:
                {
                    Toast.makeText(getApplicationContext(),"支付成功！",Toast.LENGTH_LONG).show();
                    Intent intent=new Intent(getApplicationContext(),RecordActivity.class);
                    startActivity(intent);
                    Date d=new Date();
                    for(MyFile myFile:list)
                    {
                        Record record=new Record(myFile,"打印机"+String.valueOf(id),new Date(d.getTime()+ 60000L *(id-1)*5));
                        My_List.RecordList.add(record);
                    }
                    break;
                }
            }
        }
    };

    public void addFile(View view)
    {
        ResultContract resultContract=new ResultContract();
        Intent intent=resultContract.Intent();
        startActivityForResult(intent, 2);
    }


    @SuppressLint("HandlerLeak")
    public final Handler handler=new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 0:
                {
                    updatePrice();
                }
                default:break;
            }
        }
    };


    @SuppressLint("SetTextI18n")
    public void updatePrice()
    {
        int p=20;
        if(aSwitch.isChecked()) p=30;
        int all=0;
        for(MyFile m:list)
        {
            all+=m.getPageNum()*m.getNum();
        }
        all*=p;
        total_price.setText(String.valueOf(all/100)+"."+String.valueOf(all%100));
    }
    /***
     * 回调函数
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2) {
            // 从相册返回的数据
            Log.e(this.getClass().getName(), "Result:" + data.toString());
            if (data != null) {
                // 得到图片的全路径
                Uri uri = data.getData();
                File file= MyFileUtil.uriToFileApiQ(uri,this);

                String filename=file.getName();
                String type=MyFileUtil.getFileSuffix(filename);
                Random random=new Random();
                int i=random.nextInt(50)+1;
                MyFile file1=new MyFile(filename,file,i,1,type);
                adapter.add(file1);
            }
        }
    }
}